//
//  LogExt.m
//  LearnLib
//
//  Created by Yahaong on 16/8/15.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "YHLog.h"

#ifdef DEBUG 
const DDLogLevel ddlogLevel = DDLogLevelVerbose;
#else
const DDLogLevel ddLogLevel = DDLogLevelInfo;
#endif

static DDFileLogger *fileLogger;

@interface DailyLogFormatter : DDLogFileFormatterDefault {
@private
    NSDateFormatter *_dateFormatter;
}
@end

@implementation DailyLogFormatter

- (instancetype)init
{
    self = [super init];
    if (self) {
        _dateFormatter = [[NSDateFormatter alloc] init];
        [_dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
        [_dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
    }
    return self;
}

- (NSString *)formatLogMessage:(DDLogMessage *)logMessage
{
    NSString *tstamp = nil;
    @synchronized (_dateFormatter) {
        tstamp = [_dateFormatter stringFromDate:logMessage.timestamp];
    }
    
    NSString *levelString = @"";
    switch (logMessage.flag) {
        case DDLogFlagError:
            levelString = @"ERROR";
            break;
        case DDLogFlagWarning:
            levelString = @"WARNING";
            break;
        case DDLogFlagInfo:
            levelString = @"INFO";
            break;
        case DDLogFlagDebug:
            levelString = @"DEBUG";
            break;
        case DDLogFlagVerbose:
            levelString = @"VERBOSE";
            break;
        default:
            levelString = @"UNKNOWN";
            break;
    }
    return [NSString stringWithFormat:@"%@ [ %@]<%@>%@", tstamp, logMessage.threadID, levelString, logMessage.message];
}

@end
@implementation YHLog

+ (NSString *)logFolder
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *logDirectory = [path stringByAppendingString:@"/logs/"];
    [[NSFileManager defaultManager] createDirectoryAtPath:logDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    return logDirectory;
}


+ (void)prepareForLogging
{
    DailyLogFormatter *logFormatter = [[DailyLogFormatter alloc] init];
    fileLogger = [[DDFileLogger alloc] initWithLogFileManager:[[DDLogFileManagerDefault alloc] initWithLogsDirectory:[YHLog logFolder]]];
    fileLogger.rollingFrequency = 60 * 60 *24;
    fileLogger.maximumFileSize = 1024 * 1024 * 10;  //10MB
    [fileLogger setLogFormatter:logFormatter];
    [DDLog addLogger:fileLogger];
    [[DDTTYLogger sharedInstance] setColorsEnabled:YES];
    [[DDTTYLogger sharedInstance] setLogFormatter:logFormatter];
    
    [[DDTTYLogger sharedInstance] setForegroundColor:[UIColor colorWithRed:23/255.0 green:134/ 255.0 blue:247/255.0 alpha:1.0] backgroundColor:nil forFlag:DDLogFlagInfo];
    [[DDTTYLogger sharedInstance] setForegroundColor:[UIColor redColor] backgroundColor:nil forFlag:DDLogFlagError];
    [DDLog addLogger:[DDTTYLogger sharedInstance]];
}

+ (dispatch_queue_t)loggerQueue
{
    static dispatch_queue_t queue = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        queue = dispatch_queue_create("yh.loggerQueue", NULL);
    });
    
    return queue;
}
+ (void)logWithFlag:(NSUInteger)flag format:(NSString *)format, ...
{
    va_list args;
    va_start(args, format);
    NSString *message = [[NSString alloc] initWithFormat:format arguments:args];
    va_end(args);
    
//    dispatch_async([self loggerQueue], ^{
    
        NSString* prefix = @"";
        
        switch (flag) {
            case YHLogFlagError:
                LOG_MAYBE(LOG_ASYNC_ENABLED,LOG_LEVEL_DEF, DDLogFlagError,0, nil, __PRETTY_FUNCTION__, @"%@%@", prefix , message);
                break;
            case YHLogFlagWarning:
                DDLogWarn(@"%@%@", prefix , message);
                break;
            case YHLogFlagInfo:
                DDLogInfo(@"%@%@", prefix , message);
                break;
            case YHLogFlagDebug:
                DDLogDebug(@"%@%@", prefix , message);
                break;
            case YHLogFlagVerbose:
                break;
            default:
                break;
        }
//    });
}
@end
